/**
 * Created by Administrator on 2017/9/14 0014.
 */
var common = require('../utils/common'),
    logInfoModel = require('../models/log-info'),
    _ = require('underscore'),
    logInfoBiz = {
        create: function (param) {
            var logInfo = {
                ip: param.ip||"",
                url: param.url||"",
                type: param.type,
                dateTime:  new Date(),
                remark: param.remark
            },
            strDate = common.dateFormatter(new Date,"yyyy-MM-dd"),
            self = this;

            logInfo.ip = logInfo.ip.replace("::ffff:","");

            return new Promise(function (resolve,reject) {
                self.findLogByDate(strDate).then(function(_oldLog) {
                    var oldLog = _oldLog;
                    if(!oldLog){
                        oldLog = {
                            date: strDate,
                            infoList: [logInfo]
                        };
                        logInfoModel.create(oldLog);
                    }else{
                        oldLog.infoList.push(logInfo);
                        oldLog.save(function (err) {
                            err && console.log(err);
                        });
                    }
                });
            });
        },
        findLogByDate: function(strDate){
            var param = {
                date: strDate ? strDate : common.dateFormatter(new Date(),'yyyy-MM-dd')
            };
            return new Promise(function (resolve,reject) {
                logInfoModel.find(param).exec(function(err,doc){
                    resolve(doc[0]);
                });
            });
        },
        findLogByParam: function (param) {
            var queryParam = {
                date: param.date || common.dateFormatter(new Date(),'yyyy-MM-dd')
            };
            if(!_.isEmpty(param.ip)){
                queryParam.ip = param.ip;
            }
            return new Promise(function (resolve,reject) {
                logInfoModel.find(queryParam).exec(function(err,arrModel){
                    var arrLog = [];

                    _.each(arrModel,function (model) {
                        model.infoList.length > 0 && model.infoList.sort(function (a,b) {
                            return b.dateTime-a.dateTime;
                        });
                        _.each(model.infoList,function (loginfo) {
                            arrLog.push({
                                _id: loginfo._id.toString(),
                                ip: loginfo.ip,
                                dateTime: common.dateFormatter(loginfo.dateTime),
                                url: loginfo.url,
                                type: loginfo.type,
                                remark: loginfo.remark
                            });
                        });
                    });
                    resolve(arrLog);
                });
            });
        }
    };

module.exports = logInfoBiz;